package YZ.Students.Mier.controller.tool;

import YZ.Students.Mier.Service.UserService;
import YZ.Students.Mier.Service.impl.UserServiceImpl;
import YZ.Students.Mier.controller.BaseServlet;
import YZ.Students.Mier.entity.UserEntity;
import org.apache.commons.io.IOUtils;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Part;
import java.io.FileInputStream;

/**
 * @className: FileServlet
 * @author: shuishui
 * @date: 2022/12/10 8:56
 */
@WebServlet("/FileServlet/*")
@MultipartConfig
public class FileServlet extends BaseServlet {
    private UserService userService = new UserServiceImpl();
    public String upload()throws Exception{
        Part part = req.getPart("pngFile");
        String fileName = part.getSubmittedFileName();
        String pngPath = req.getServletContext().getRealPath("png");
        System.out.println(pngPath);
        String filePath = pngPath+"\\"+"fileName";
        part.write(filePath);
        UserEntity loginUser = (UserEntity) req.getSession().getAttribute("loginUser");
        userService.updateHeaderUrl(loginUser.getId(),filePath);
        return outJson(true);
    }
    public void showPng()throws Exception{
        String url = req.getParameter("fileName");
        String png = req.getServletContext().getRealPath("png");
        FileInputStream ips = new FileInputStream(png+"//"+url);
        IOUtils.copy(ips,resp.getOutputStream());
    }
}
